package com.example.sso.clientA.controller;

import cn.dev33.satoken.config.SaSsoConfig;
import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.sso.SaSsoProcessor;
import cn.dev33.satoken.sso.SaSsoUtil;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.dtflys.forest.Forest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务聚合
 *
 * @version [V2.0]
 * @package com.example.sso.clientA.controller;
 * @description:[用一句话描述该文件做什么]
 * @author: [zhuben]
 * @date: [2024-01-23 18:21]
 * @copyright: 2024 echovin.com
 */

@RestController
public class SsoClientController {

    @RequestMapping("/")
    public String index2(@RequestParam("client") String client) {
        String str = "<h2>Sa-Token SSO-Client"+client+" 应用端</h2>" +
                "<p>当前会话是否登录：" + StpUtil.isLogin() + "</p>" +
                "<p><a href=\"javascript:location.href='/sso/login?back=' + encodeURIComponent(location.href);\">登录</a>" +
                " <a href='/sso/logout?back=self'>注销</a></p>";
        return str;
    }

    @RequestMapping("/sso/*")
    public Object ssoRequest() {
        return SaSsoProcessor.instance.clientDister();
    }

    // 平台化首页
    @RequestMapping("/home")
    public Object index() {
        // 如果未登录，则先去登录
        if(!StpUtil.isLogin()) {
            return SaHolder.getResponse().redirect("/sso/login?back=http://127.0.0.1:8083/home");
        }

        // 拼接各个子系统的地址，格式形如：/sso/auth?redirect=${子系统首页}/sso/login?back=${子系统首页}
        String link1 = "http://127.0.0.1:8082/sso/auth?redirect=http://127.0.0.1:8083/sso/login?back=http://127.0.0.1:8083?client=A";
        String link2 = "http://127.0.0.1:8082/sso/auth?redirect=http://127.0.0.1:8084/sso/login?back=http://127.0.0.1:8084?client=B";
        String link3 = "http://127.0.0.1:8082/sso/auth?redirect=http://127.0.0.1:8085/sso/login?back=http://127.0.0.1:8085?client=C";
        String link4 = "/sso/logout?back=self";
        // 组织网页结构返回到前端
        String title = "<h2>SSO 平台首页</h2>";
        String session = "<p>当前会话是否登录：" + StpUtil.isLogin() + "</p>";
        String client1 = "<p><a href='" + link1 + "' target='_blank'> 进入ClientA系统 </a></p>";
        String client2 = "<p><a href='" + link2 + "' target='_blank'> 进入ClientB系统 </a></p>";
        String client3 = "<p><a href='" + link3 + "' target='_blank'> 进入ClientC系统 </a></p>";
        String client4 = "<p><a href='" + link4 + "' target='_blank'> 注销 </a></p>";

        return title + session + client1 + client2 + client3 +client4;
    }


    @RequestMapping("/sso/myInfo")
    public Object myInfo() {
        // 组织请求参数
        Map<String, Object> map = new HashMap<>();
        map.put("apiType", "userinfo");
        map.put("loginId", StpUtil.getLoginId());

        // 发起请求
        Object resData = SaSsoUtil.getData(map);
        System.out.println("sso-server 返回的信息：" + resData);
        return resData;
    }

    @RequestMapping("/logout")
    public void logout(){
        StpUtil.logout(StpUtil.getLoginId());
    }

    @RequestMapping("getPermission")
    public SaResult getPermission() {
        // 查询权限信息 ，如果当前会话未登录，会返回一个空集合
        List<String> permissionList = StpUtil.getPermissionList();
        System.out.println("当前登录账号拥有的所有权限：" + permissionList);

        // 查询角色信息 ，如果当前会话未登录，会返回一个空集合
        List<String> roleList = StpUtil.getRoleList();
        System.out.println("当前登录账号拥有的所有角色：" + roleList);

        // 返回给前端
        return SaResult.ok()
                .set("roleList", roleList)
                .set("permissionList", permissionList);
    }

    @Autowired
    private void configSso(SaSsoConfig sso) {
        // ... 其他代码
        // 配置 Http 请求处理器
        sso.setSendHttp(url -> {
            System.out.println("------ 发起请求：" + url);
            return Forest.get(url).executeAsString();
        });
    }
}
